import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_note/note/base.dart';
import 'package:flutter_note/note/bean.dart';
import 'package:flutter_note/note/const.dart';
import 'package:flutter_note/note/manager.dart';
import 'package:flutter_note/note/page_note_detail.dart';
import 'package:flutter_note/note/utils.dart';
import 'package:flutter_note/note/widgets.dart';

class NotePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _NotePageState();
  }
}

class _NotePageState extends BaseState<NotePage> {
  NoteManager _noteManager = NoteManager.getInstance();
  List<ReplyNote> _noteList = NoteManager.getInstance().getNoteList();
  EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _initData();
    _controller = EasyRefreshController();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _initData() async {
    if (EmptyUtils.isEmpty(_noteList)) {
      //如果列表是空的,先从服务器拉取,如果有内容直接显示
      await _noteManager.loadData(); //必须要await,不然拉取了不能更新
      _noteList = NoteManager.getInstance().getNoteList();
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldkey,
      backgroundColor: color.bg_page,
      body: _body(),
    );
  }

  Widget _body() {
    if (EmptyUtils.isEmpty(_noteList)) {
      return Container();
    } else {
      return EasyRefresh(
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        controller: _controller,
        header: ClassicalHeader(),
        footer: ClassicalFooter(),
        onRefresh: _refresh,
        onLoad: _loadMore,
        child: ListView.builder(
            //列表控件
            itemCount: _noteList.length,
            itemBuilder: (BuildContext context, int position) {
              return _buildItem(_noteList[position]);
            }),
      );
    }
  }

  Future _loadMore() async {
    Reply reply = await _noteManager.loadMoreData(); //必须要await,不然拉取了不能更新
    print("_loadMore.reply.code = " + reply.code.toString());
    if (reply.code == Const.SUCCESS) {
      setState(() {});
    } else if (reply.code == Const.FAIL || reply.code == Const.FAIL_NONE) {
      Future.delayed(Duration(milliseconds: 500)).then((val) {
        setState(() {});
      });
    } else {
      setState(() {
        showMessage(reply.msg);
      });
    }
  }

  Future _refresh() async {
    Reply reply = await _noteManager.loadData(); //必须要await,不然拉取了不能更新
    if (reply.code != Const.SUCCESS) {
      setState(() {
        showMessage(reply.msg);
      });
    } else {
      _noteList = _noteManager.getNoteList();
      Future.delayed(Duration(milliseconds: 500)).then((val) {
        setState(() {});
      });
    }
  }

  //ListView的Item
  Widget _buildItem(ReplyNote note) {
    return NoteView(
      note,
      () {
        if (_noteManager.getCurrentNote() != note) {
          _noteManager.setCurrentNote(note); //和之前的bid不一样的时候需要重新赋值
          CommentManager.getInstance().getCommentList().clear();
        }
        startPage(NoteDetailPage());
      },
    );
  }
}
